package com.lzhl.buildjar.dao.impl;

import com.lzhl.buildjar.dao.BaseDao;
import com.lzhl.buildjar.dao.SettingDao;
import com.lzhl.buildjar.exception.DaoException;
import com.lzhl.buildjar.po.Category;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/3/29
 */
public class SettingDaoImpl extends BaseDao implements SettingDao {

    @Override
    public Map<String, String> get(Set<String> keys) {
        Map<String, String> settings = new HashMap<String, String>();
        if(keys.size() == 0) {
            return settings;
        }

        Connection conn;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("select * from T_Setting \n");
            sql.append("where " + getIDSql("FKey", keys) + " \n");

            pstmt = conn.prepareStatement(sql.toString());

            rs = pstmt.executeQuery();
            while (rs.next()) {
                settings.put(rs.getString("FKey"), rs.getString("FValue"));
            }
        }
        catch (Exception e) {
            throw new DaoException("设置查询失败", e);
        }
        finally {
            close(pstmt, rs);
        }

        return settings;
    }

    @Override
    public void update(Map<String, String> settings) {
        if(settings.size() == 0) {
            return;
        }

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();

            StringBuffer sql = new StringBuffer();
            sql.append("delete from T_Setting \n");
            sql.append("where " + getIDSql("FKey", settings.keySet()) + " \n");

            pstmt = conn.prepareStatement(sql.toString());

            pstmt.executeUpdate();
            pstmt.close();

            sql.setLength(0);
            sql.append("insert into T_Setting \n");
            sql.append("(FKey, FValue) \n");
            sql.append("values \n");
            sql.append("(?, ?) \n");

            pstmt = conn.prepareStatement(sql.toString());

            for(Map.Entry<String, String> setting : settings.entrySet()) {
                pstmt.setString(1, setting.getKey());
                pstmt.setString(2, setting.getValue());
                pstmt.addBatch();
            }
            pstmt.executeBatch();

            conn.commit();
        }
        catch (Exception e) {
            rollback(conn);
            throw new DaoException("设置更新失败", e);
        }
        finally {
            close(pstmt);
        }
    }

    @Override
    public void update(String key, String value) {
        Map<String, String> settings = new HashMap<String, String>();
        settings.put(key, value);
        update(settings);
    }

    @Override
    public String get(String key) {
        Set<String> keys = new HashSet<String>();
        keys.add(key);
        Map<String, String> settings = get(keys);
        if(settings.size() > 0) {
            return settings.values().iterator().next();
        }
        return "";
    }

}
